package com.quarters.controller.admin;


import com.quarters.bean.Quarters;
import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.ztree.ZtreeBean;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 小区表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Controller
@RequestMapping("admin/quarters")
public class QuartersController extends BaseController {


    /**
     * @return 小区管理添加
     */
    @RequestMapping(value = "quarters_add.html",method = RequestMethod.GET)
    public String quarters_add(){
        return "admin/quarters/quarters_add";
    }
    /**
     * @return 小区管理编辑
     */
    @RequestMapping(value = "quarters_edit.html",method = RequestMethod.GET)
    public String quarters_edit(){
        return "admin/quarters/quarters_edit";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Quarters> selectMyPage(VoPage voPage,String quartersName){
        return quartersService.selectMyPage(voPage,quartersName);
    }


    /**
     * @param quarters
     * @return 添加
     */
    @RequestMapping(value = "saveQuarters",method = RequestMethod.POST)
    @ResponseBody
    public Map saveQuarters(@RequestBody Quarters quarters){
        quarters.setCreateTime(System.currentTimeMillis());
        if(quartersService.save(quarters)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    /**
     * @param quarters
     * @return 编辑
     */
    @RequestMapping(value = "updateByIdQuarters",method = RequestMethod.POST)
    @ResponseBody
    public Map updateByIdQuarters(@RequestBody Quarters quarters){
        if(quartersService.updateById(quarters)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    /**
     * @param quarters
     * @return 添加
     */
    @RequestMapping(value = "deleteByIdQuarters",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteByIdQuarters(@RequestBody Quarters quarters){
        if(quartersService.removeById(quarters)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    /**
     * @return 查询所有
     */
    @RequestMapping(value = "selectList")
    @ResponseBody
    public List selectList(){
        return quartersService.list();
    }


    /**
     * @param session
     * @return 根据对应的角色查询对应的小区
     */
    @RequestMapping(value = "llist")
    @ResponseBody
    public List<ZtreeBean> llist(HttpSession session){
        if(getUserInfo(session).getUsername().equals("admin")){
            return quartersService.allQuarters();
        }
        return quartersService.llist(getUserInfo(session).getId());
    }

}
